<?php

class BackEndController extends CController {
    public $layout = 'main';
    public $menu = array();
    public $breadcrumbs = array();
    public $layoutPageTitle = '';
    public $layoutPageDescription = '';

    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        return array();
    }

    public function createUrl($route, $params = array(), $ampersand = '&') {
        if ($route === '')
            $route = $this->getId() . '/' . $this->getAction()->getId();
        elseif (strpos($route, '/') === false)
            $route = $this->getId() . '/' . $route;
        if ($route[0] !== '/' && ($module = $this->getModule()) !== null)
            $route = $module->getId() . '/' . $route;
        $route = 'backend.php/' . $route;
        return Yii::app()->createUrl(trim($route, '/'), $params, $ampersand);
    }

    public function templateConfigs() {
        $config = Yii::app()->configuration->backendTemplateConfig;
        return array(
            'header' => $config['header'],
            'sidebar' => $config['sidebar'],
            'side_content' => $config['sidebarContent'],
            'page' => $config['page'],
            'theme' => $config['theme'],
            'active_page' => HSUrl::backendUrl() . '/' . Yii::app()->controller->id
        );
    }

    public function menuBarItem() {
        if (Yii::app()->user->id > 0) {
            $arr = array(
                array(
                    'name' => 'Logout',
                    'url' => HSUrl::backendLogoutUrl(),
                ),
                array(
                    'name' => 'Trang chính',
                    'url' => HSUrl::backendDefaultUrl()
                ),
                array(
                    'name' => 'Công việc của bạn',
                    'url' => HSUrl::backendTaskForCurrentUserUrl()
                ),
            );

            if (Yii::app()->user->isAdmin() == "true") {
                array_push($arr, array(
                    'name' => 'Quản lý task',
                    'url' => HSUrl::backendTaskUrl()
                ));

                array_push($arr, array(
                    'name' => 'Quản lý user',
                    'url' => HSUrl::backendUsersUrl()
                ));
            }
            return $arr;

        } else {
            return array(
                array(
                    'name' => 'Login',
                    'url' => HSUrl::backendLoginUrl(),
                ));
        }

    }

    public function cLinkPagerWidgetOption($pages, $maxButCount = 6) {
        return array(
            'pages' => $pages,
            'header' => '',
            'maxButtonCount' => $maxButCount,
            'cssFile' => FALSE,
            'firstPageLabel' => '<i class="icon-double-angle-left"></i> First',
            'lastPageLabel' => 'Last <i class="icon-double-angle-right"></i> ',
            'selectedPageCssClass' => 'active',
            'previousPageCssClass' => 'prev',
            'prevPageLabel' => '<i class="icon-angle-left"></i> Prev',
            'nextPageCssClass' => 'next',
            'nextPageLabel' => 'Next <i class="icon-angle-right"></i>',
            'hiddenPageCssClass' => 'disabled',
        );
    }

    public function _response($data) {
        header('Content-type: application/json');
        echo json_encode($data);
        Yii::app()->end();
    }

}